package cn.tanghom.app.oa.service;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.flowable.common.engine.impl.el.AbstractFlowableFunctionDelegate;

import com.unbiz.common.StringUtil;
import com.unbiz.common.convert.primitive.IntegerConverter;



public class MatchFunctionDelegate extends AbstractFlowableFunctionDelegate {
	    
	@Override
	public String prefix() {		
		return "str";
	}

	@Override
	public String localName() {		
		return "match";
	}

	@Override
	public Method functionMethod() {		
		return getTwoObjectParameterMethod();
	}

	@Override
	public Class<?> functionClass() {
	
		return this.getClass();
	}
	

	/**
	 * 字符串相似度比较，
	 * 比较两个项目是否匹配，匹配返回true，其中一项为空返回false
	 * 
	 * 如：zhongguo ~  zhongguo,riben
	 * @param a 简历属性
	 * @param b 职位属性，
	 * @return
	 */
	public static boolean match(Object a, Object b){
		return _match(a,b,false);
	}

	/**
	 * 字符串相似度比较
	 * 
	 * 如：zhongguo ~  zhongguo,riben
	 * @param a 简历属性
	 * @param b 职位属性，
	 * @param matchEmpty 为空是返回什么
	 * @return
	 */
	
	public static boolean _match(Object a, Object b,boolean matchEmpty){
		//有一个为空，则认为匹配
		if(a==null || b==null) return matchEmpty; 
		
		if(a instanceof List){
			List alist = (List) a;
			for(Object i: alist){
				if(_match(i,b,false)) return true;
			}
			return false;
		}
		
		if(b instanceof List){
			List blist = (List) b;
			for(Object i: blist){
				if(_match(a,i,false)) return true;
			}
			return false;
		}
		
		String astr = a.toString();
		String bstr = b.toString();		
		if(astr.isEmpty() || bstr.isEmpty()) return matchEmpty; 
		
		//有包含关系，则认为匹配
		if(bstr.contains(astr) || astr.contains(bstr)) return true;
		
		double n = stringEditDistance(astr,bstr);
		if(n/(astr.length()+bstr.length())<0.35){
			return true;
		}
		return false;
	}
	
	 public static int stringEditDistance(String source, String target)  
	    {  
	        char[] s=source.toCharArray();  
	        char[] t=target.toCharArray();  
	        int slen=source.length();  
	        int tlen=target.length();  
	        int d[][]=new int[slen+1][tlen+1];  
	        for(int i=0;i<=slen;i++){  
	            d[i][0]=i;  
	        }  
	        for(int i=0;i<=tlen;i++) {  
	            d[0][i]=i;  
	        }  
	        for(int i=1;i<=slen;i++){  
	            for(int j=1;j<=tlen;j++) {  
	                if(s[i-1]==t[j-1]) {  
	                    d[i][j]=d[i-1][j-1];  
	                }else{  
	                    int insert=d[i][j-1]+1;  
	                    int del=d[i-1][j]+1;  
	                    int update=d[i-1][j-1]+1;  
	                    d[i][j]= Math.min(insert, del)>Math.min(del, update)?Math.min(del, update):Math.min(insert, del);  
	                }  
	            }  
	        }  
	        return d[slen][tlen];  
	    } 
	 
	 public static String getRegexString(String line, Pattern p) {
	        if (StringUtil.isBlank(line)) {
	            return "";
	        }
	        try {
	            Matcher ma = p.matcher(line.replaceAll("\\s+", " "));
	            while (ma.matches()) {
	                return ma.group(1);
	            }
	            return ma.group(1);
	        } catch (Exception e) {
	            return "";
	        }
	    }
}
